using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;
    private Vector2 movement;
    // public PlayerGun playerGun;
    private Camera mainCamera;
    private Animator animator;
    public Transform playerBody;

    private float speed;//移动速度
    public bool isDead;
    public ParticleSystem deathParticle;//死亡特效
    public float shakeDuration = 0.5f;
    public float shakeMagnitude = 0.1f;
    private VariableJoystickMove joystickMove;
    private VariableJoystickShoot JoystickShoot;

    void Start()
    {
        Time.timeScale = 1;
        speed = PlayerStats.instance.speed;
        PlayerStats.instance.health = PlayerStats.instance.startHealth;//TODO先设定默认开始血量等于总血

        rb = GetComponent<Rigidbody2D>();
        mainCamera = Camera.main;
        animator = GetComponentInChildren<Animator>();
        StartCoroutine(AddHealthCoroutine());
        joystickMove = ResourceManager.instance.joystickMove;
        JoystickShoot = ResourceManager.instance.JoystickShoot;
        ResourceManager.instance.SetGoldNumberText(PlayerStats.instance.GoldNumber);
    }
    void Update()
    {
        //获取虚拟摇杆的位置
        float horizontalInput = joystickMove.Horizontal;
        float verticalInput = joystickMove.Vertical;
        movement = new Vector2(horizontalInput, verticalInput).normalized;
        UpdateRotation();
        // UpdateMouse();
        UpdateAnimation(horizontalInput, verticalInput);
        if (isDead)
        {
            Invoke("GameOver", 0.3f); //延迟1秒调用
        }
    }

    //自动回血
    IEnumerator AddHealthCoroutine()
    {
        while (true)
        {
            yield return new WaitForSeconds(PlayerStats.instance.addHealthTime);
            if (PlayerStats.instance.health < PlayerStats.instance.startHealth)
            {
                PlayerStats.instance.health = Mathf.Min(PlayerStats.instance.health + PlayerStats.instance.addHealth, PlayerStats.instance.startHealth);
                //加血飘字
                ResourceManager.instance.GetaddHealthText(transform.position, PlayerStats.instance.addHealth);
            }
        }
    }

    private void FixedUpdate()
    {
        //移动代码
        rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
    }

    //旋转
    private void UpdateRotation()
    {
        //获取虚拟摇杆的位置
        float horizontalInput = JoystickShoot.Horizontal;
        float verticalInput = JoystickShoot.Vertical;
        if (horizontalInput != 0)
        {
            //计算了一个向量与 y 轴正方向之间的夹角
            float angle = Mathf.Atan2(horizontalInput, verticalInput) * Mathf.Rad2Deg;
            //Vector3.forward 表示 z 轴正方向,方法创建了一个绕 z 轴旋转 angle 度的旋转四元数
            playerBody.rotation = Quaternion.AngleAxis(-angle, Vector3.forward);
            //持续按下射击
            animator.SetBool("isShoot", true);
            ResourceManager.instance.playerGun.OnDownFire(rb);
        }
        else
        {
            animator.SetBool("isShoot", false);
            ResourceManager.instance.playerGun.OnUpFire();
        }

    }

    //玩家面向鼠标方向,如果需要的话可以开启
    private void UpdateMouse()
    {
        Vector3 mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition); // 获取鼠标在世界坐标系下的位置
        Vector2 shootDirection = mousePosition - playerBody.position;//鼠标的屏幕坐标-玩家屏幕坐标=玩家面向鼠标位置的向量
        float angle = Mathf.Atan2(shootDirection.x, shootDirection.y) * Mathf.Rad2Deg;
        playerBody.rotation = Quaternion.AngleAxis(-angle, Vector3.forward);
    }

    //播放动画
    private void UpdateAnimation(float horizontalInput, float verticalInput)
    {
        if (movement.x != 0 || movement.y != 0)
        {
            animator.SetBool("isWalk", true);
        }
        else
        {
            animator.SetBool("isWalk", false);
        }
    }

    //受击
    public void TakeDamage(float damage)
    {
        StartCoroutine(Shake());
        PlayerStats.instance.health -= damage;
        if (PlayerStats.instance.health <= 0 || isDead)
        {
            isDead = true;
            //死亡特效
            if (deathParticle == null) { Debug.Log("没有特效"); return; };
            ParticleSystem ps = Instantiate(deathParticle, transform.position, Quaternion.identity);
            Destroy(ps.gameObject, ps.main.duration);
        }
    }

    //人物闪烁
    public IEnumerator Shake()
    {
        // 定义抖动效果的持续时间
        float shakeDuration = 0.2f;

        // 定义闪烁效果的持续时间
        float blinkDuration = 0.1f;

        // 定义闪烁效果之间的间隔
        float blinkInterval = 0.05f;

        // 获取SpriteRenderer组件
        SpriteRenderer spriteRenderer = playerBody.GetComponent<SpriteRenderer>();

        // 在抖动效果的持续时间内，在起始位置和随机位置之间进行插值
        for (float t = 0; t < shakeDuration; t += Time.deltaTime)
        {
            // 闪烁角色
            if (t % blinkInterval < blinkDuration)
            {
                spriteRenderer.enabled = !spriteRenderer.enabled;
            }
            else
            {
                spriteRenderer.enabled = true;
            }
            yield return null;
        }
        // 确保Sprite可见
        spriteRenderer.enabled = true;
    }

    private void GameOver()
    {
        //停止游戏
        Time.timeScale = 0;
        //弹出结束菜单
        ResourceManager.instance.gameOverMenu.SetActive(true);
        Destroy(gameObject);
    }
}
